/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace Galaktika.BI.Silverlight.ClientApp.Web.Runtime.DataAccess
{
    using Galaktika.BI.Silverlight.ClientApp.Web.Runtime.DataAccess;

    public static class PivotHelper
    {
        public static DataTable GetDataTable(object value)
        {
            if (value is DataView)
            {
                return ((DataView)value).Table;
            }
            if (value is DataTable)
            {
                return (DataTable)value;
            }
            if (value is DataSet)
            {
                DataSet ds = (DataSet)value;
                if (ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
            }

            return null;
        }

        public static bool IsPivotTable(object value)
        {
            if (value is DataTable)
            {
                foreach (DataColumn dc in ((DataTable)value).Columns)
                {
                    if (dc.ExtendedProperties["PIVOT_AREA_STRING"] != null)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        public static IDictionary<string, Dictionary<string, double>> GetPivotSeriesData(DataTable table)
        {
            if (table != null)
            {
                string seriesColumnName = string.Empty;
                string dataColumnName = string.Empty;
                foreach (DataColumn dc in table.Columns)
                {
                    switch (GetArea(dc))
                    {
                        case "RowArea":
                            seriesColumnName = dc.ColumnName;
                            break;
                        case "DataArea":
                            dataColumnName = dc.ColumnName;
                            break;
                        default:
                            break;
                    }
                }

                Dictionary<string, Dictionary<string, double>> series = new Dictionary<string, Dictionary<string, double>>();
                foreach (DataRow dr in table.Rows)
                {
                    string seriesName = dr[seriesColumnName].ToString();

                    Dictionary<string, double> points;
                    series.TryGetValue(seriesName, out points);
                    if (points == null)
                    {
                        points = new Dictionary<string, double>();
                        series.Add(seriesName, points);
                    }

                    string name = GetPointName(dr);
                    double value = 0;
                    object rowVal = dr[dataColumnName];
                    if (rowVal != null && !DBNull.Value.Equals(rowVal))
                    {
                        value += Convert.ToDouble(rowVal);
                    }

                    if (points.ContainsKey(name))
                    {
                        points[name] = points[name] + value;
                    }
                    else
                    {
                        points.Add(name, value);
                    }
                }

                return series;
                //foreach (KeyValuePair<string, Dictionary<string, double>> seriesData in series)
                //{
                //    Series serie = new Series(seriesData.Key, ViewType.Bar);
                //    chart.Series.Add(serie);
                //    foreach (KeyValuePair<string, double> pointData in seriesData.Value)
                //    {
                //        serie.Points.Add(new SeriesPoint(pointData.Key, new double[] { pointData.Value }));
                //    }
                //}
            }

            return new Dictionary<string, Dictionary<string, double>>();
        }

        private static string GetPointName(DataRow dr)
        {
            StringBuilder sb = new StringBuilder();
            foreach (DataColumn dc in dr.Table.Columns)
            {
                if (GetArea(dc) == "ColumnArea")
                {
                    if (sb.Length > 0)
                    {
                        sb.Append('.');
                    }
                    sb.Append(dr[dc].ToString());
                }
            }

            return sb.ToString();
        }

        private static string GetArea(DataColumn dc)
        {
            string area = "FilterArea";
            object ext = dc.ExtendedProperties["PIVOT_AREA_STRING"];
            if (ext != null)
            {
                try
                {
                    area = ext.ToString();
                }
                catch (ArgumentException)
                {
                }
            }

            return area;
        }
    }
}
